/**
New chat object to generate html for chat clients
*/
function chat(room, user, updated){
	this.chatRoom = room;
	this.chatUser = user;
	this.lastUpdated = updated;
	
	this.setChatRoom = function(room){
		this.chatRoom = room;
	};
	
	this.setUser = function(user){
		this.chatUser = user;
	}
	
	this.getHTML = function(){
		var toReturn = 
		"<div id=\"chat_window_" + this.chatRoom + "\">\n" +
			"\t<div class=\"header_wrapper\">Chat Room " + this.chatRoom + "<img class=\"settings\" src=\"./images/cog.png\" onclick=\"showMenu(" + this.chatRoom + ")\" />\n" +
			"\t</div>\n" +
			"\t\t<ul id=\"settings_" + this.chatRoom + "\" style=\"display:none\">\n" +
			"\t\t\t<li id=\"invite_" + this.chatRoom + "\" class=\"invite_user\">Invite</li>\n" +
			"\t\t</ul>\n" +
			"\t<textarea readonly class=\"chat_text\"></textarea>\n" +
			"\t<div id=\"chat_form_" + this.chatRoom + "\" class=\"chat_form\">\n" +
				"\t\t<input type=\"hidden\" class=\"chat_update_time\" name=\"chat_update_time\" value=\"" + this.lastUpdated + "\" />\n" +
				"\t\t<input type=\"hidden\" class=\"chat_room\" name=\"chat_room\" value=\"" + this.chatRoom + "\" />\n" +
				"\t\t<input type=\"hidden\" class=\"chat_user\" name=\"chat_user\" value=\"" + this.chatUser + "\" />\n" +
				"\t\t<input type=\"text\" class=\"chat_input\" name=\"chat_input\" />\n" +
				"\t\t<input type=\"button\" class=\"send_button\" value=\"Send!\" onclick=\"sendClick(" + this.chatRoom + ")\" />\n" +
			"\t</div>\n" +
		"\t</div>\n";
		return toReturn;
	}
}

/**
Function used to invite user
*/
var invite = function(chatRoom){
	var user = prompt("Please enter username to invite", "");
	jQuery.ajax({
            type:'POST',
            url: './process/processInvite.php',
            data: "username=" + user + "&chatRoom=" +chatRoom,
            success:function(data){
				var response = data;
				if(response <= "0"){
					alert("User could not be invited.  Please retype username.");
				}else{
					alert("User has been invited.");
				}
			}
    });
}

/**
Function used to close chat
*/
var close = function(chatRoom){
	console.log("init");
	jQuery.ajax({
            type:'POST',
            url: './process/processLeaveChat.php',
            data: "chat_room=" +chatRoom,
            success:function(data){
				//console.log(data)
				window.location = "./index.php";
			}
    });
}

/**
Function used to show menu
*/
var showMenu = function(chatRoom){
	//jQuery("#settings_" + chatRoom).show();
	jQuery.ajax({
            type:'POST',
            url: './settings.php',
            data: "chat_room=" +chatRoom,
            success:function(data){
				jQuery("body").append(data);
			}
    });
}

/**
Function sued to hide menu
*/
var hideMenu = function(chatRoom){
	jQuery("#settings_" + chatRoom).hide();
}

/**
Function used to place onclick on send button
*/
var sendClick = function(chatRoom){
	var chatRoom = jQuery("#chat_form_" + chatRoom + " .chat_room").val();
	var chatInput = jQuery("#chat_form_" + chatRoom + " .chat_input").val();
	var chatUpdated = jQuery("#chat_form_" + chatRoom + " .chat_update_time").val();
	data = {chat_room:chatRoom, chat_input:chatInput};
	jQuery.post("./process/processChat.php", data)
	jQuery("#chat_form_" + chatRoom + " .chat_input").val("");
}